import React from 'react';
import {Router, Route, Switch, Redirect} from 'dva/router';
import {LocaleProvider} from 'antd';
import zhCN from 'antd/lib/locale-provider/zh_CN';
import BasicLayout from './layouts/BasicLayout';
import UserLayout from './layouts/UserLayout';
import Exception403 from './routes/Exception/403';
import Exception404 from './routes/Exception/404';
import Exception500 from './routes/Exception/500';

const PrivateRoute = ({component: Component, ...rest}) => (
  <Route
    {...rest}
    render={props => (
            sessionStorage.getItem('access_token') ? (
              <Component {...props} />
            ) : (
              <Redirect to={{
                    pathname: '/login',
                    state: {from: props.location},
                }}
              />
            )
        )}
  />
);

function RouterConfig({history}) {
    return (
      <LocaleProvider locale={zhCN}>
        <Router history={history}>
          <Switch>
            <Route path="/login" component={UserLayout} />
            <PrivateRoute path="/" component={BasicLayout} />
            <Route component={Exception404} />
          </Switch>
        </Router>
      </LocaleProvider>
    );
}

export default RouterConfig;
